package com.dgshanger.sanhxiaofeisc.adapter

import com.amap.api.maps2d.AMapUtils
import com.amap.api.maps2d.model.LatLng
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.NearbyItemEntity
import com.dgshanger.sanhxiaofeisc.ui.home.utils.ImageLoader
import java.math.BigDecimal

/**
 * Created by JacobHHH on 2018/5/4.
 */
class CategoryAdapter(layoutResId : Int,data : List<NearbyItemEntity>) : BaseQuickAdapter<NearbyItemEntity,BaseViewHolder>(layoutResId,data) {

    override fun convert(helper: BaseViewHolder?, item: NearbyItemEntity?) {
        ImageLoader.loadToUrl(mContext,helper!!.getView(R.id.iv_nearby), Constants.getBaseUrl()+item?.menu_img)
        helper.setText(R.id.tv_nearby_name,item!!.shop_name)
        helper.setText(R.id.tv_nearby_grade,mContext.getString(R.string.tv_grade)+item.comment_score)
        helper.setText(R.id.tv_nearby_category,item.category_name)
        helper.setText(R.id.tv_nearby_introduce,item.introduce)
        val location = Constants.getLocation()
        val myLatLng = LatLng(location[0].toDouble(), location[1].toDouble())
        val shopLatLng = LatLng(item.latitude.toDouble(), item.longitude.toDouble())
        val distanceMeters = AMapUtils.calculateLineDistance(myLatLng, shopLatLng).toDouble()
        val scale = BigDecimal(distanceMeters / 1000).setScale(2, BigDecimal.ROUND_DOWN)
        helper.setText(R.id.tv_nearby_distance,scale.toString()+"KM")
    }
}